using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using BitMango;
using BitMango.Diagnostics;
using UnityEngine;

namespace BMCommon
{
    public static class Push
    {
        static Push()
        {
            if (Push.impl == null)
            {
                Push.impl = FIRMImpl.Create();
            }
            int @int = PlayerPrefs.GetInt("push_noti", 0);
            Push.Enable = (@int == 1);
        }

        public static bool Enable
        {
            get
            {
                return Push.enable;
            }
            set
            {
                Push.enable = value;
                int value2 = (!Push.enable) ? 0 : 1;
                PlayerPrefs.SetInt("push_noti", value2);
            }
        }

        private static string GetNotificationValue()
        {
            if (Push.enable)
            {
                return "1";
            }
            if (!Push.enable)
            {
                return "0";
            }
            Debugger.Assert(false, "BMCommon.Push enable error");
            return "0";
        }

        private static void ProcessMessageData(IDictionary<string, string> dic)
        {
            Push.Msg msg = new Push.Msg(dic);
            if (msg.IsWrong)
            {
                Debugger.Assert(false, "---- BMCommon.Push _  Message is wrong format");
            }
            else if (Push.onMsgReceived != null)
            {
                Push.onMsgReceived(msg);
            }
        }


        public static void ProcessGainItem(Push.Msg msg)
        {
            Wallet.Gain(msg.item, msg.amount, null);
            Wallet.Save();
        }

        public static void AddMessageHandler(Action<Push.Msg> handler)
        {
            Push.onMsgReceived = (Action<Push.Msg>)Delegate.Combine(Push.onMsgReceived, handler);
        }

        public static void RemoveMessageHandler(Action<Push.Msg> handler)
        {
            Push.onMsgReceived = (Action<Push.Msg>)Delegate.Remove(Push.onMsgReceived, handler);
        }

        private static FIRMImpl impl;

        private static bool enable;

        private static Action<Push.Msg> onMsgReceived;

        public class Msg
        {
            public Msg(IDictionary<string, string> dic)
            {
                this.isWrong = true;
                this.title = dic["title"];
                this.desc = dic["desc"];
                this.item = dic["item"];
                if (Wallet.Get(this.item) == null)
                {
                    Debugger.Assert(false, "BMCommon.Push _ itemKey is wrong");
                    return;
                }
                int num = -1;
                try
                {
                    num = int.Parse(dic["amount"]);
                }
                catch (FormatException)
                {
                    Debugger.Assert(false, "BMCommon.Push _ amount value _ wrong format");
                    return;
                }
                catch (OverflowException)
                {
                    Debugger.Assert(false, "BMCommon.Push _ amount value _ overflow");
                    return;
                }
                if (num <= 0)
                {
                    Debugger.Assert(false, "BMCommon.Push _ item amount <= 0");
                    return;
                }
                this.amount = num;
                this.isWrong = false;
            }

            public bool IsWrong
            {
                get
                {
                    return this.isWrong;
                }
            }

            private bool isWrong;

            public string title;

            public string desc;

            public string item;

            public int amount;
        }
    }
}
